<?php
declare (strict_types = 1);

namespace app\controller;

use app\BaseController;
use app\model\DispatchLists;
use app\model\pomain_arrival_log;
use app\model\T_PM_MO_BASE;
use app\model\T_PM_PROJECT_BASE;
use app\model\T_PM_PROJECT_DETAIL;
use app\model\T_WMS_STOCK_ITEM;


class Operatingcockpit extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        $ServerOperating=(new \app\Server\Operatingcockpit());
        $datas=$ServerOperating->index();
        //U8-MES库存差异
        $newCy = $datas['newCy'];
        //生产工单清单
        $list=$datas['list'];
        $newClient=[];
        foreach ($list['data'] as $k=>$v){
            $name=$v['CUST_CODE'];
            if(array_key_exists($name,$newClient)){
                $new[$name]=$v['CUST_CODE'];
            }else{
                $new[$name]=$v['CUST_CODE'];
            }
        }
        $newsClient=[];
        foreach ($newClient as $k=>$v){
            $newsClient[]=$v;
        }
        //工单统计计算数据
        $data2=$datas['data2'];
        //呆滞料
        $data4=$datas['data3'];
        return view('/operatingcockpit/index',['data'=>$list,'data2'=>$data2,'data3'=>$newCy,'client'=>$newsClient,'data4'=>$data4]);
    }
    /**
     * 生产部管理指标
     */
    public function produce(){
        $request=$this->request->param('day','','trim');
        $ServerOperating=(new \app\Server\Operatingcockpit());
        $list=$ServerOperating->produce($request);
        return json(['data'=>$list]);
    }
    /**
     * 是否超期的数据
     */
    public function produceCq(){
        $request=$this->request->param('cq');
        $T_PM_MO_BASE=(new T_PM_MO_BASE());
        $DispatchLists=(new DispatchLists());
        //生产工单
        $list=(new T_PM_PROJECT_BASE())->index();
        foreach($list as $k=>$v){
            $list[$k]['cSoCode']=$DispatchLists->index($v['MODEL_CODE']);
        }
        foreach($list as $k=>$v){
            //判断工单是否超期,根据状态判断，正常关结，
            if($v['STATUS']==3 || $v['STATUS']==4){
                if($v['PLAN_END_TIME']!='' && $v['ACTUAL_END_TIME']!=''){
                    if($v['PLAN_END_TIME']>$v['ACTUAL_END_TIME']){
                        $list[$k]['cq']="<span style='color: white;background-color: green'>否</span>";
                    }else{
                        $list[$k]['cq']="<span style='color: white;background-color: red'>是</span>";
                    }
                }elseif ($v['PLAN_END_TIME']!= ''&& $v['ACTUAL_END_TIME']==''){
                    if($v['PLAN_END_TIME']<date('Y-m-d')){
                        $list[$k]['cq']="<span style='color: white;background-color: red'>是</span>";
                    }else{
                        $list[$k]['cq']="<span style='color: white;background-color: green'>否</span>";
                    }
                }else{
                    $list[$k]['cq']="<span style='color: white;background-color: green'>否</span>";
                }
            }elseif ($v['STATUS']==1 || $v['STATUS']==2){
                if($v['PLAN_END_TIME']<date('Y-m-d')){
                    $list[$k]['cq']="<span style='color: white;background-color: red'>是</span>";
                }else{
                    $list[$k]['cq']="<span style='color: white;background-color: green'>否</span>";
                }
            }
            //判断状态
            switch($v['STATUS']){
                case 1:$list[$k]['STATUS']='开立';break;
                case 2:$list[$k]['STATUS']='投产';break;
                case 3:$list[$k]['STATUS']='关结';break;
                case 4:$list[$k]['STATUS']='强制关结';break;
            }
            //计算每个工单对应制令单的不良率
            $counts=$T_PM_MO_BASE->where('PROJECT_ID',$v['ID'])->sum('INPUT_QTY');
            $counts2=$T_PM_MO_BASE->where('PROJECT_ID',$v['ID'])->sum('OUTPUT_QTY');
            if($counts2!=0){
                $str=1-intval($counts2)/intval($counts);
                $list[$k]['NgCount']=substr((string)$str,0,6);
            }else{
                $list[$k]['NgCount']=0;
            }
            //日期截取
            $list[$k]['CREATE_TIME']=substr($v['CREATE_TIME'],0,16);
            if($v['CLOSE_TIME']!='')$list[$k]['CLOSE_TIME']=substr($v['CLOSE_TIME'],0,16);
            if($v['ACTUAL_END_TIME']!='')$list[$k]['ACTUAL_END_TIME']=substr($v['ACTUAL_END_TIME'],0,16);
        }
        $new=[];
        if($request==0){
            return json(['data'=>$list]);
        }elseif ($request==1){
            foreach ($list as $k=>$v){
                if($v['cq']=="<span style='color: white;background-color: red'>是</span>"){
                    $new[]=$v;
                }
            }
        }elseif ($request==2){
            foreach ($list as $k=>$v){
                if($v['cq']=="<span style='color: white;background-color: green'>否</span>"){
                    $new[]=$v;
                }
            }
        }
        return json(['data'=>$new]);
    }
    /**
     * 查看制令单
     */
    public function modoc(){
        $request=$this->request->param('','','trim');
        //获取制令单信息
        $list=(new T_PM_MO_BASE())->where('PROJECT_ID',$request['ID'])
            ->field('PROJECT_NO,MO_DOC,STATUS,ACTUAL_ONLINE_TIME,ACTUAL_START_TIME,ACTUAL_END_TIME,CLOSE_TIME,CREATE_TIME')
            ->select();
        if(!empty($list)) $list->toArray();

        foreach ($list as $k=>$v){
            //判断状态
            switch($v['STATUS']){
                case 1:$list[$k]['STATUS']='开立';break;
                case 2:$list[$k]['STATUS']='投产';break;
                case 3:$list[$k]['STATUS']='关结';break;
                case 4:$list[$k]['STATUS']='暂停';break;
                case 5:$list[$k]['STATUS']='强制关结';break;
            }
            $list[$k]['CREATE_TIME']=substr($v['CREATE_TIME'],0,16);
            if($list[$k]['ACTUAL_START_TIME']!='')$v['ACTUAL_START_TIME']=substr($v['ACTUAL_START_TIME'],0,16);
            if($list[$k]['ACTUAL_END_TIME']!='')$v['ACTUAL_END_TIME']=substr($v['ACTUAL_END_TIME'],0,16);
            if($list[$k]['ACTUAL_ONLINE_TIME']!='')$v['ACTUAL_ONLINE_TIME']=substr($v['ACTUAL_ONLINE_TIME'],0,16);
        }

        return view('/operatingcockpit/MoDoc',['data'=>$list]);
    }
    /**
     * 查看采购单信息
     */
    public function procurement(){
        $request=$this->request->param('','','trim');
        //获取采购单信息
        $list=(new T_PM_PROJECT_DETAIL())->alias('a')
            ->leftJoin('T_WMS_PO_DETAIL b','a.ITEM_CODE=b.ITEM_CODE')
            ->leftJoin('T_WMS_PO_INFO c','b.PO_DOC=c.PO_DOC')
            ->where('a.PROJECT_NO','=',$request['PROJECT_NO'])
            ->field('a.PROJECT_NO,b.PO_DOC,c.STATUS,c.CREATE_TIME,c.UPDATE_TIME')->select();
        if(!empty($list))$list->toarray();
        $new=[];
        foreach ($list as $k=>$v){
            $name=$v['PO_DOC'];
            if(array_key_exists($name,$new)){
                $new[$name]=$v;
            }else{
                $new[$name]=$v;
            }
        }
        $news=[];
        foreach ($new as $k=>$v){
            $news[]=$v;
        }
        //获取计划倒料时间，辅助mes中获取
        foreach ($news as $k=>$v){
            switch($v['STATUS']){
                case 1:$news[$k]['STATUS']='开立';break;
                case 2:$news[$k]['STATUS']='操作中';break;
                case 3:$news[$k]['STATUS']='关闭';break;
            }
            $news[$k]['update_time']=(new pomain_arrival_log())->where('cPOID','=',$v['PO_DOC'])->value('update_time');
            //时间截取
            if(!empty($news[$k]['CREATE_TIME']))$news[$k]['CREATE_TIME']=substr($v['CREATE_TIME'],0,16);
            if(!empty($news[$k]['UPDATE_TIME']))$news[$k]['UPDATE_TIME']=substr($v['UPDATE_TIME'],0,16);
            if(!empty($news[$k]['update_time']))$news[$k]['update_time']=substr($news[$k]['update_time'],0,16);
        }
        return view('/operatingcockpit/procurement',['data'=>$news,'count'=>count($news)]);
    }
    /**
     * 查看生产工单IQC信息
     */
    public function pidIqc(){
        $request=$this->request->param('PROJECT_NO','','trim');
        //根据产品料号关联查询iqc信息
        $list=(new T_PM_PROJECT_DETAIL())->alias('a')
            ->leftJoin('T_QM_INSPECT_INFO b','a.ITEM_CODE=b.ITEM_CODE')
            ->where('a.PROJECT_NO',$request)
            ->where('b.TEST_STEP','=',1)
            ->field('a.PROJECT_NO,b.DOC_NO,b.ITEM_CODE,b.TEST_RESULT,b.TEST_START_TIME,b.TEST_END_TIME')
            ->select()->toarray();
        foreach ($list as $k=>$v){
            switch($v['TEST_RESULT']){
                case 1:$list[$k]['TEST_RESULT']='OK';break;
                case 2:$list[$k]['TEST_RESULT']='NG';break;
            }
        }
        return view('/operatingcockpit/IQC',['data'=>$list,'count'=>count($list)]);
    }
    /**
     * 查看生产工单FQC信息
     */
    public function pidFqc(){
        $request=$this->request->param('PROJECT_NO','','trim');
        //根据产品料号关联查询iqc信息
        $list=(new T_PM_PROJECT_DETAIL())->alias('a')
            ->leftJoin('T_QM_INSPECT_INFO b','a.ITEM_CODE=b.ITEM_CODE')
            ->where('a.PROJECT_NO',$request)
            ->where('b.TEST_STEP','=',2)
            ->field('a.PROJECT_NO,b.DOC_NO,b.ITEM_CODE,b.TEST_RESULT,b.TEST_START_TIME,b.TEST_END_TIME')
            ->select()->toarray();
        foreach ($list as $k=>$v){
            switch($v['TEST_RESULT']){
                case 1:$list[$k]['TEST_RESULT']='OK';break;
                case 2:$list[$k]['TEST_RESULT']='NG';break;
            }
        }
        return view('/operatingcockpit/FQC',['data'=>$list,'count'=>count($list)]);
    }
    /**
     * 生产工单明细
     */
    public function operatingDetail(){
        $ID=$this->request->param('ID','','trim');
        $T_PM_MO_BASE=(new T_PM_MO_BASE());
        $list=(new T_PM_PROJECT_BASE())
            ->where('ID','=',$ID)
            ->field('ID,PROJECT_NO,STATUS,PLAN_END_TIME,CREATE_TIME,ACTUAL_END_TIME,CLOSE_TIME,ACTUAL_START_TIME')
            ->find();
        if(!empty($list))$list=$list->toArray();
        //判断工单是否超期,根据状态判断，正常关结，
        if($list['STATUS']==3 || $list['STATUS']==4){
            if($list['PLAN_END_TIME']!='' && $list['ACTUAL_END_TIME']!=''){
                if($list['PLAN_END_TIME']>$list['ACTUAL_END_TIME']){
                    $list['cq']="<span style='color: white;background-color: green'>否</span>";
                }else{
                    $list['cq']="<span style='color: white;background-color: red'>是</span>";
                }
            }elseif ($list['PLAN_END_TIME']!= ''&& $list['ACTUAL_END_TIME']==''){
                if($list['PLAN_END_TIME']<date('Y-m-d')){
                    $list['cq']="<span style='color: white;background-color: red'>是</span>";
                }else{
                    $list['cq']="<span style='color: white;background-color: green'>否</span>";
                }
            }else{
                $list['cq']="<span style='color: white;background-color: green'>否</span>";
            }
        }elseif ($list['STATUS']==1 || $list['STATUS']==2){
            if($list['PLAN_END_TIME']<date('Y-m-d')){
                $list['cq']="<span style='color: white;background-color: red'>是</span>";
            }else{
                $list['cq']="<span style='color: white;background-color: green'>否</span>";
            }
        }
            //判断状态
            switch($list['STATUS']) {
                case 1:
                    $list['STATUS'] = '开立';
                    break;
                case 2:
                    $list['STATUS'] = '投产';
                    break;
                case 3:
                    $list['STATUS'] = '关结';
                    break;
                case 4:
                    $list['STATUS'] = '强制关结';
                    break;
            }
            //计算每个工单对应制令单的不良率
            $counts=$T_PM_MO_BASE->where('PROJECT_ID',$list['ID'])->sum('INPUT_QTY');
            $counts2=$T_PM_MO_BASE->where('PROJECT_ID',$list['ID'])->sum('OUTPUT_QTY');
            if($counts2!=0){
                $list['NgCount']=intval($counts2)/intval($counts);
            }else{
                $list['NgCount']=0;
            }
            //工单准备周期=工单开立时间-制令单下达时间
            $list['gd_zb_xd_time']=$T_PM_MO_BASE->where('PROJECT_ID',$list['ID'])->where('ACTUAL_ONLINE_TIME','<>','')->field('min(ACTUAL_ONLINE_TIME)')->value('ACTUAL_ONLINE_TIME');
            //采购周期
                 $list['cgOpen_Time']=(new T_PM_PROJECT_DETAIL())->alias('a')//工单BOM信息表
                ->leftJoin('T_WMS_PO_DETAIL b','a.ITEM_CODE=b.ITEM_CODE')//PM-采购单明细表
                ->leftJoin('T_WMS_PO_INFO c','b.PO_DOC=c.PO_DOC')//PM-采购单据表
                ->where('a.PROJECT_NO','=',$list['PROJECT_NO'])
                ->where('c.OPEN_TIME','<>','')
                ->field('c.OPEN_TIME')->order('OPEN_TIME','asc')->value('OPEN_TIME');
                $list['cgCLOSE_Time']=(new T_PM_PROJECT_DETAIL())->alias('a')//工单BOM信息表
                ->leftJoin('T_WMS_PO_DETAIL b','a.ITEM_CODE=b.ITEM_CODE')//PM-采购单明细表
                ->leftJoin('T_WMS_PO_INFO c','b.PO_DOC=c.PO_DOC')//PM-采购单据表
                ->where('a.PROJECT_NO','=',$list['PROJECT_NO'])
                ->field('c.ARRIVAL_TIME')->order('ARRIVAL_TIME','desc')->value('ARRIVAL_TIME');
            //制造周期,工单实际生产时间-实际完工时间
            $list['zld_end_time']=$list['ACTUAL_START_TIME'];
            $list['zld_end']=$list['ACTUAL_END_TIME'];
        //日期截取
        $list['CREATE_TIME']=substr($list['CREATE_TIME'],0,16);
        if($list['CLOSE_TIME']!='')$list['CLOSE_TIME']=substr($list['CLOSE_TIME'],0,16);
        if($list['ACTUAL_END_TIME']!='')$list['ACTUAL_END_TIME']=substr($list['ACTUAL_END_TIME'],0,16);
        if($list['zld_end_time']!='')$list['zld_end_time']=substr($list['zld_end_time'],0,16);
        if($list['zld_end']!='')$list['zld_end']=substr($list['zld_end'],0,16);
        if($list['cgOpen_Time']!='')$list['cgOpen_Time']=substr($list['cgOpen_Time'],0,16);
        if($list['cgCLOSE_Time']!='')$list['cgCLOSE_Time']=substr($list['cgCLOSE_Time'],0,16);
        if($list['gd_zb_xd_time']!='')$list['gd_zb_xd_time']=substr($list['gd_zb_xd_time'],0,16);
            return view('/operatingcockpit/detail',['data'=>$list]);
        }


}
